<script setup>
import FirstTitle from "@comp/FirstTitle";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { onMounted } from "vue";
import { searchDataZhbz } from "@/api/search";
import { addOrUpdateZhbz } from "@/api/addOrUpdate";

const city = window.globalObj.name;
const editorRef = shallowRef();
const editorRef1 = shallowRef();

const toolbarConfig = {};
const toolbarConfig1 = {};
const editorConfig = {
  placeholder: "请输入内容...",
};
const mode = ref("default");
const mode1 = ref("default");

// 加载html
const initHtml = () => {
  searchDataZhbz({
    m: "交通运输保障",
  }).then((res) => {
    const { data } = res;
    valueHtml.value =
      data.find((x) => x.k === "协调对接机场车站等重要交通节点的组织实施方法")
        ?.v || "";
    valueHtml1.value =
      data.find((x) => x.k === "筹措调配交通运力的组织实施方法")?.v || "";
  });
};

const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例，重要！
};
const handleCreated1 = (editor) => {
  editorRef1.value = editor; // 记录 editor 实例，重要！
};

const valueHtml = ref(
  "以市人防指挥部协调对接保障为主，不足部分逐级向上级申请、调拨或经费补助。"
);

const valueHtml1 = ref(
  "一指挥协调全市交通运输力量保障为主，不足部分逐级向上级申请、调拨或经费补助。"
);

const saveProgramme = () => {
  ElMessageBox.confirm(`确定要保存吗?`, "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    customClass: "message-box",
  }).then(() => {
    addOrUpdateZhbz({
      m: "交通运输保障",
      data: [
        {
          k: "协调对接机场车站等重要交通节点的组织实施方法",
          v: valueHtml.value,
        },
        {
          k: "筹措调配交通运力的组织实施方法",
          v: valueHtml1.value,
        },
      ],
    })
      .then((result) => {
        if (result.code === 200) {
          ElMessage.success("操作成功");
          initHtml();
        } else {
          ElMessage.error(result.msg);
        }
      })
      .catch(() => {});
  });
};

onMounted(() => {
  initHtml();
});
onBeforeUnmount(() => {
  const editor = editorRef.value;
  const editor1 = editorRef1.value;
  if (!editor) return;
  if (!editor1) return;
  editor.destroy();
  editor1.destroy();
});
</script>
<template>
  <div class="">
    <div class="top-15">
      主要明确协调对接机场
      、车站等重要交通节点和筹措调配交通运力等工作的组织实施方法。
    </div>
    <FirstTitle
      name="协调对接机场、车站等重要交通节点的组织实施方法"
      class="top-15"
    />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef"
        :defaultConfig="toolbarConfig"
        :mode="mode"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml"
        :defaultConfig="editorConfig"
        :mode="mode"
        @onCreated="handleCreated"
      />
    </div>

    <FirstTitle name="筹措调配交通运力的组织实施方法" class="top-15" />
    <div style="border: 1px solid #ccc" class="top-15">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editorRef1"
        :defaultConfig="toolbarConfig1"
        :mode="mode1"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="valueHtml1"
        :defaultConfig="editorConfig"
        :mode="mode1"
        @onCreated="handleCreated1"
      />
    </div>

    <div class="flex-justify-end top-20">
      <el-button type="primary" class="save-btn" @click="saveProgramme"
        >保存</el-button
      >
    </div>
  </div>
</template>
<style scoped lang="scss"></style>